//
//  BTCbuyTableViewController.m
//  project
//
//  Created by 张亚林 on 2017/10/30.
//  Copyright © 2017年 Macmini. All rights reserved.
//

#import "BTCbuyTableViewController.h"

#import "TuoGuanSelectHeader.h"
#import "TuoGuanSelectCell.h"

#import "LiuYanCell.h"
#import "LiuyanHeader.h"
#define  cellFooter (10)
@interface BTCbuyTableViewController ()<UITableViewDelegate,UITableViewDataSource>{
    BOOL switchOn;
    
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIImageView *image0;
@property (weak, nonatomic) IBOutlet UIImageView *image1;
@property (weak, nonatomic) IBOutlet UIImageView *image2;
@end

@implementation BTCbuyTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.estimatedRowHeight = 20;
    self.image1.image = [self drawXLineByImageView:self.image1];
     self.image2.image = [self drawXLineByImageView:self.image2];
    self.image0.image = [self drawYLineByImageView:self.image0];
//    self.tableView.contentOffset = CGPointMake(40, 40);
//   self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
}



- (UIImage *)drawYLineByImageView:(UIImageView *)imageView{
    UIGraphicsBeginImageContext(imageView.frame.size); //开始画线 划线的frame
    [imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
    //设置线条终点形状
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    // 5是每个虚线的长度 1是高度
    CGFloat lengths[] = {3,1};
    CGContextRef line = UIGraphicsGetCurrentContext();
    // 设置颜色
    CGContextSetStrokeColorWithColor(line, [UIColor colorWithWhite:0.408 alpha:1.000].CGColor);
    CGContextSetLineDash(line, 0, lengths,2); //画虚线
    CGContextMoveToPoint(line, 0,0); //开始画线
    CGContextAddLineToPoint(line, 0, imageView.frame.size.height);
    /*
     CGContextSetLineDash(context, 0, lengths,2);
     CGContextMoveToPoint(context, 0, 0);
     CGContextAddLineToPoint(context, 0,_height);
     */
    CGContextStrokePath(line);
    // UIGraphicsGetImageFromCurrentImageContext()返回的就是image
    return UIGraphicsGetImageFromCurrentImageContext();
}
// 返回虚线image的方法
- (UIImage *)drawXLineByImageView:(UIImageView *)imageView{
    UIGraphicsBeginImageContext(imageView.frame.size); //开始画线 划线的frame
    [imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
    //设置线条终点形状
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    // 5是每个虚线的长度 1是高度
    CGFloat lengths[] = {3,1};
    CGContextRef line = UIGraphicsGetCurrentContext();
    // 设置颜色
    CGContextSetStrokeColorWithColor(line, [UIColor colorWithWhite:0.408 alpha:1.000].CGColor);
    CGContextSetLineDash(line, 0, lengths, 2); //画虚线
    CGContextMoveToPoint(line, 0.0, 1.0); //开始画线
    CGContextAddLineToPoint(line, imageView.width -5, 1);
    
    CGContextStrokePath(line);
    // UIGraphicsGetImageFromCurrentImageContext()返回的就是image
    return UIGraphicsGetImageFromCurrentImageContext();
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        if (switchOn) {
            return 5;
        } else{
            return 0;
        }
    }
    
    return 10;
}


-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return 80   ;
    } else{
        
        return 60;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return 80   ;
    } else{
        
        return UITableViewAutomaticDimension;
    }
    
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return cellFooter; ;
    } else{
        
        return 0.001;
    }
   
    
}

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        TuoGuanSelectHeader *cell = [tableView dequeueReusableCellWithIdentifier:@"TuoGuanSelectHeader"];
        cell.headerSwitch.on = switchOn;
        cell.switchBlock = ^(BOOL on) {
            switchOn = on;
            [self.tableView reloadData];
            
        };
        return cell;
    } else{
        LiuyanHeader *cell = [tableView dequeueReusableCellWithIdentifier:@"LiuyanHeader"];
        return cell;
        
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section ==0) {
        TuoGuanSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TuoGuanSelectCell"];
        return cell;
    } else {
        LiuYanCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LiuYanCell"];
        return cell;
    }
}


@end
